package com.zijie.shopgoodsapi.goods.service.impl;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.zijie.brand.model.Brand;
import com.zijie.category.model.Category;
import com.zijie.core.util.JsonResult;
import com.zijie.goods.model.Goods;
import com.zijie.goods.model.search.GoodSearchBean;
import com.zijie.goods.service.GoodService;
import com.zijie.shopgoodsapi.goods.mapper.GoodMapper;
import com.zijie.shopgoodsapi.goods.service.BrandFeignService;
import com.zijie.shopgoodsapi.goods.service.CategoryFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GoodServiceImpl implements GoodService {
    @Autowired
    private GoodMapper goodMapper;
    @Autowired
    private BrandFeignService brandFeignService;
    @Autowired
    private CategoryFeignService categoryFeignService;

    @Override
    public Page<Goods> findAll(Page<Goods> page, GoodSearchBean gsb) {
        Page<Goods> result = goodMapper.findAll(page,gsb);
        result.getRecords().forEach(goods -> {
            ResponseEntity<JsonResult<Category>> resultResponseEntity = this.categoryFeignService.findById(goods.getCategoryId());
            //观察下游服务是否可用
            System.out.println("resultResponseEntity = " + resultResponseEntity);
            JsonResult<Category> jsonResult = resultResponseEntity.getBody();
            if (jsonResult!=null){
                goods.setCategory(jsonResult.getData());
            }

//            品牌
            ResponseEntity<JsonResult<Brand>> resultResponseEntity1 = this.brandFeignService.findById(goods.getBrandId());
            JsonResult<Brand> jsonResult1 = resultResponseEntity1.getBody();
            if (jsonResult1!=null){
                goods.setBrand(jsonResult1.getData());
            }

        });
        return result;
    }

    @Override
    public Goods findById(Integer id) {
        return goodMapper.selectById(id);
    }

    @Override
    public boolean save(Goods goods) {
        return goodMapper.insert(goods)>0;
    }

    @Override
    public boolean update(Goods goods) {
        return goodMapper.updateById(goods)>0;
    }

    @Override
    public int deleteByIds(List<Integer> ids) {
        return goodMapper.deleteBatchIds(ids);
    }

    @Override
    public Object deleteById(Integer id) {
        return goodMapper.deleteById(id);
    }

    @Override
    public boolean updateHotStatus(Integer id, Integer newStatus) {
        return goodMapper.updateHotStatus(id,newStatus);
    }

    @Override
    public boolean updatePublicStatus(Integer id, Integer isTakeDown) {
        return goodMapper.updatePublicStatus(id,isTakeDown);
    }


}
